package com.mall.ibatis.persistence.sqlmapdao;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ibatis.dao.client.DaoManager;
import com.mall.ibatis.bean.Brand;
import com.mall.ibatis.bean.JqgridParams;
import com.mall.ibatis.persistence.iface.BrandDao;

public class BrandSqlMapDao extends BaseSqlMapDao implements BrandDao {

	public BrandSqlMapDao(DaoManager daoManager) {
		super(daoManager);
	}

	@SuppressWarnings("unchecked")
	public List<Brand> showBrands(JqgridParams params) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("sidx", params.getSidx());
		map.put("sord", params.getSord());
		map.put("criterion", params.getCriterion());
		return queryForList("showBrands", map);
	}

	public int showBrandsCount(JqgridParams params) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("sidx", params.getSidx());
		map.put("sord", params.getSord());
		map.put("criterion", params.getCriterion());
		return (Integer)queryForObject("showBrandsCount", map);
	}

	public String addBrand(Brand brand) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("BRAND_ID", brand.getBRAND_ID());
		map.put("BRAND_NAME", brand.getBRAND_NAME());
		map.put("BRAND_ADDRESS", brand.getBRAND_ADDRESS());
		map.put("BRAND_LOGO", brand.getBRAND_LOGO());
		map.put("BRAND_CONTACT", brand.getBRAND_CONTACT());
		map.put("BRAND_TEL", brand.getBRAND_TEL());
		map.put("BRAND_ACTIVE", brand.getBRAND_ACTIVE());
		map.put("BRAND_FLOOR", brand.getBRAND_FLOOR());
		map.put("BRAND_ORDER", brand.getBRAND_ORDER());
		map.put("BRAND_UPDATE_TIME", brand.getBRAND_UPDATE_TIME());
		try {
			update("addBrand",map);
		} catch (Exception e) {
			return e.getMessage();
		}
		
		return "";
	}

	public String deleteBrand(String rowID) throws SQLException {
		int i = delete("deleteBrand",rowID);
		if(i>0){
			return "删除成功";
		}
		return "删除失败";
	}

	public String saveBrand(List<Brand> brList) {
		try {
			daoManager.startTransaction();
			for(Brand br:brList){
				Map<String,Object> map = new HashMap<String,Object>();
				map.put("BRAND_ID", br.getBRAND_ID());
				map.put("BRAND_NAME", br.getBRAND_NAME());
				map.put("BRAND_ADDRESS", br.getBRAND_ADDRESS());
				map.put("BRAND_LOGO", br.getBRAND_LOGO());
				map.put("BRAND_CONTACT", br.getBRAND_CONTACT());
				map.put("BRAND_TEL", br.getBRAND_TEL());
				map.put("BRAND_ACTIVE", br.getBRAND_ACTIVE());
				map.put("BRAND_FLOOR", br.getBRAND_FLOOR());
				map.put("BRAND_ORDER", br.getBRAND_ORDER());
				map.put("BRAND_UPDATE_TIME", br.getBRAND_UPDATE_TIME());
				update("saveBrand", map);
			}
			daoManager.commitTransaction();
		} catch (Exception e) {
			return e.getMessage();
		}finally{
			daoManager.endTransaction();
		}
		return "";
	}

}
